package com.bwf.service;

import com.bwf.exception.DepositException;
import com.bwf.exception.RegistException;
import com.bwf.pojo.Account;
import com.bwf.pojo.SavingAccount;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/11 9:28
 * @Copyright 博为峰
 */
public class ATM {
    public Scanner scanner;
    public Bank bank;

    public ATM() {
        scanner = new Scanner(System.in);
        bank = new Bank();
    }

    public void service() throws SQLException, IOException, ClassNotFoundException {
        do {
            System.out.println("1.存钱2.取钱3.查询4.退出.5.注册");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    System.out.println("请输入卡号");
                    String cardNo = scanner.nextLine();
                    System.out.println("请输入密码");
                    String password = scanner.nextLine();
                    System.out.println("请输入金额");
                    double money = Double.parseDouble(scanner.nextLine());
                    Account account = bank.verify(cardNo, password);
                    if (account != null) {
                        bank.save(cardNo, password, money);
                    } else {
                        System.out.println("卡号或密码错误");
                    }
                    break;
                case "2":
//                    System.out.println("请输入取钱的金额");
//                    double depMoney = Double.parseDouble(scanner.nextLine());
//                    try {
//                        account.deposit(depMoney);
//                    } catch (DepositException e) {//DepositException e = new DepositException("余额不足！！！");
//                        System.out.println(e.getMessage());
//                    }
//                    break;
                case "3":
//                    SavingAccount savingAccount = bank.query(account.getCardNo());
//                    System.out.println("账户余额：" + savingAccount.getBalance());
//                    break;
                case "4":
//                    flag = true;
//                    break;
                case "5":
                    System.out.println("请输入卡号");
                    String cardN = scanner.nextLine();
                    System.out.println("请输入密码");
                    String pwd = scanner.nextLine();
                    System.out.println("请输入金额");
                    double mon = Double.parseDouble(scanner.nextLine());
                    try {
                        bank.register(cardN, pwd, mon);
                    } catch (RegistException | SQLException e) {
                        System.out.println("注册失败");
                    }
                    break;
                default:
                    System.out.println("无效的输入");
            }
        } while (true);
    }

    /*public void service() {
        System.out.println("请输入卡号");
        String cardNo = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        boolean flag = false;
        //alt + enter
        Account account = null;
        try {
            account = bank.verify(cardNo, password);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (account != null) {
            do {
                System.out.println("1.存钱2.取钱3.查询4.退出.5.注册");
                String choice = scanner.nextLine();
                switch (choice) {
                    case "1":
                        System.out.println("请输入存钱的金额");
                        double money = Double.parseDouble(scanner.nextLine());
                        account.save(money);
                        break;
                    case "2":
                        System.out.println("请输入取钱的金额");
                        double depMoney = Double.parseDouble(scanner.nextLine());
                        try {
                            account.deposit(depMoney);
                        } catch (DepositException e) {//DepositException e = new DepositException("余额不足！！！");
                            System.out.println(e.getMessage());
                        }
                        break;
                    case "3":
                        SavingAccount savingAccount = bank.query(account.getCardNo());
                        System.out.println("账户余额：" + savingAccount.getBalance());
                        break;
                    case "4":
                        flag = true;
                        break;
                    case "5":
                        System.out.println("请输入卡号");
                        String cardN = scanner.nextLine();
                        System.out.println("请输入密码");
                        String pwd = scanner.nextLine();
                        System.out.println("请输入金额");
                        double mon = Double.parseDouble(scanner.nextLine());
                        try {
                            bank.register(cardN, pwd, mon);
                        } catch (RegistException e) {
                            System.out.println("注册失败");
                        }
                        break;
                    default:
                        System.out.println("无效的输入");
                }
            } while (!flag);
        }
    }*/
}
